<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;

class Answer extends Model
{
    //
    public function add()
    {
      if(!user_ins()->is_logged_in())
      return ['status'=>0 ,'msg'=> 'login  requeired'];

      if(!rq('question_id') || !rq('content'))
        return ['status'=>0 ,'msg'=> 'question_id and content are requeired'];
      $question=question_ins()->find(rq('question_id'));

      if (!$question)
       return ['status'=>0 ,'msg'=> 'question is not exists'];

      $answered = $this
    ->where(['question_id' => rq('question_id'),'user_id' => session('user_id')])
    ->count();

    if($answered)

      return ['status'=>0 ,'msg'=> ' duplicate question'];

    $this->content = rq('content');
    $this->question_id =rq('question_id');
    $this->user_id = session('user_id');
    return $this->save() ?
     ['status'=>1,'id' => $this->id] :
     ['status'=>0,'msg' => 'db insert fauiled'];

    }

    public function change()
    {
      if(!user_ins()->is_logged_in())
      return ['status'=>0 ,'msg'=> 'login  requeired'];

      if(!rq('id'))
      return ['status'=>0 ,'msg'=> 'id  requeired'];


      $answer = $this->find(rq('id'));
      if($answer->user_id != session('user_id'))
      ['status'=>0 ,'msg'=> 'permissin denned'];
      if (!rq('content'))
      return
      ['status'=>0 ,'msg'=> 'not change'];

      $answer->content=rq('content');
      return $answer->save() ?
      ['status'=>1] :
      ['status'=>0,'msg' => 'db update fauiled'];

      }


      public function read()
      {
         if(!rq('id') && !rq('question_id'))
         return ['status'=>0,'msg'=>'id or question_id is requeired'];
         if(rq('id'))
         {
           $answer = $this->find(rq('id'));
           if(!$answer)
           return ['status'=>0,'msg'=>'quesiton is not exits'];
              return ['status'=>1,'data'=>$answer];
         }
         if(!question_ins()->find(rq('question_id')))
          return ['status'=>0,'msg'=>'quesiton is not exits'];
         $answer = $this->where('question_id', rq('question_id'))
         ->get()
         ->keyBy('id');
        return ['status'=>1,'data'=>$answer];

      }

      public function vote()
      {
       if(!user_ins()->is_logged_in())
       return  ['status'=>0 ,'msg'=> 'login  requeired'];
       if(!rq('id')  ||!rq('vote'))
       return  ['status'=>0 ,'msg'=> 'id  and vote are  requeired'];

      /**/
      $answer = $this->find(rq('id'));
      if(!$answer) return ['status'=>0 ,'msg'=> 'answer  not exists'];

    $vote = rq('vote') <=1 ? 1:2;
        $answer
      ->users()
      ->newPivotStatement()
      ->where('user_id',session('user_id'))
      ->where('answer_id', rq('id'))
      ->delete();

      $answer
      ->users()
      ->attach(session('user_id'),['vote' =>$vote]);

        return ['status'=>1];


      }

      public function users()
      {
        return $this->belongsToMany('App\Http\Models\User')
        ->withPivot('vote')
        ->withTimestamps();
      }
}
